package cn.site.modules.sys.entity;

import cn.site.core.utils.IdGen;
import cn.site.core.utils.StringUtils;
import com.google.common.collect.Lists;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;

/**
 * Created by ZGJ on 2015/3/8.
 */
@Entity
@Table(name = "sys_role")
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String id;
    @Length(min = 1, max = 100)
    @Column(name = "name", nullable = false)
    private String name;    // 角色名称

    @ManyToMany(mappedBy = "roleList", fetch = FetchType.LAZY)
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<User> userList = Lists.newArrayList(); // 拥有用户列表

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_menu",
            joinColumns = {@JoinColumn(name = "role_id")},
            inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<Menu> menuList = Lists.newArrayList(); // 拥有菜单列表

    @PrePersist
    public void prePersist() {
        this.id = IdGen.uuid();
    }

    public Role() {
        super();
    }

    public Role(String id, String name) {
        this();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;

    }

    @Transient
    public List<String> getUserIdList() {
        List<String> nameIdList = Lists.newArrayList();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return nameIdList;
    }

    @Transient
    public String getUserIds() {
        List<String> nameIdList = Lists.newArrayList();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @Transient
    public List<String> getMenuIdList() {
        List<String> menuIdList = Lists.newArrayList();
        for (Menu menu : menuList) {
            menuIdList.add(menu.getId());
        }
        return menuIdList;
    }

    @Transient
    public void setMenuIdList(List<String> menuIdList) {
        menuList = Lists.newArrayList();
        for (String menuId : menuIdList) {
            Menu menu = new Menu();
            menu.setId(menuId);
            menuList.add(menu);
        }
    }

    @Transient
    public String getMenuIds() {
        List<String> nameIdList = Lists.newArrayList();
        for (Menu menu : menuList) {
            nameIdList.add(menu.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @Transient
    public void setMenuIds(String menuIds) {
        menuList = Lists.newArrayList();
        if (menuIds != null){
            String[] ids = StringUtils.split(menuIds, ",");
            for (String menuId : ids) {
                Menu menu = new Menu();
                menu.setId(menuId);
                menuList.add(menu);
            }
        }
    }

    /**
     * 获取权限字符串列表
     */
    @Transient
    public List<String> getPermissions() {
        List<String> permissions = Lists.newArrayList();
        for (Menu menu : menuList) {
            if (menu.getPermission()!=null && !"".equals(menu.getPermission())){
                permissions.add(menu.getPermission());
            }
        }
        return permissions;
    }

    @Transient
    public boolean isAdmin(){
        return isAdmin(this.id);
    }

    @Transient
    public static boolean isAdmin(String id){
        return id != null && id.equals("1");
    }
}
